package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcOrdgroupEntryEntity;
import com.dhcc.cdc.busi.service.ICdcOrdgroupEntryService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 医嘱套明细
 * 
 * @ClassName: CdcOrdgroupEntryController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:07
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdcordgroupentry")
public class CdcOrdgroupEntryController extends BaseController {

	private String PREFIX = "/backend/cdc/cdcordgroupentry/";

	@Autowired
	private ICdcOrdgroupEntryService cdcOrdgroupEntryService;
	

    /**
     * 跳转到医嘱套明细列表首页
     */
    @RequestMapping("")
    @Permission("cdcordgroupentry:list")
    public String index() {
        return PREFIX + "cdcordgroupentry.html";
    }
	
    /**
     * 跳转到新增医嘱套明细
     */
    @RequestMapping("/to_add")
    @Permission("cdcordgroupentry:add")
    public String toAdd() {
        return PREFIX + "cdcordgroupentry_add.html";
    }
	
	/**
     * 跳转到修改医嘱套明细
     */
    @RequestMapping("/to_update/{cdcOrdgroupEntryId}")
    @Permission("cdcordgroupentry:update")
    public String toUpdate(@PathVariable Long cdcOrdgroupEntryId, Model model) {
        CdcOrdgroupEntryEntity cdcOrdgroupEntry = this.cdcOrdgroupEntryService.selectById(cdcOrdgroupEntryId);
        model.addAttribute("cdcOrdgroupEntry",cdcOrdgroupEntry);
        return PREFIX + "cdcordgroupentry_edit.html";
    }

    /**
     * 获取医嘱套明细列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdcordgroupentry:list")
    public Object list(@RequestParam Map<String, Object> params) {
        List<CdcOrdgroupEntryEntity> result = null;
        Page<CdcOrdgroupEntryEntity> page = new PageFactory<CdcOrdgroupEntryEntity>().defaultPage();
        if((CommonUtil.isNotEmpty(params.get("NotQuery")))&&(params.get("NotQuery").toString().equals("true"))){
            result = new ArrayList<CdcOrdgroupEntryEntity>();
        }else{
            result = this.cdcOrdgroupEntryService.page(page,params);
        }
        page.setRecords((List<CdcOrdgroupEntryEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增医嘱套明细
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdcordgroupentry:add")
    @BussinessLog(value = "新增医嘱套明细")
    public Object add(CdcOrdgroupEntryEntity cdcOrdgroupEntry) {
        if (CommonUtil.isOneEmpty(cdcOrdgroupEntry)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcOrdgroupEntry.insert();
        return R.ok();
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    @Permission("cdcordgroupentry:add")
    public Object save(@RequestParam Long ordGroupId, @RequestParam String ordIds) {
        String[] ordIdArr = ordIds.split(":");
        for (String ordId : ordIdArr) {
            if(CommonUtil.isNotEmpty(ordId)){
                CdcOrdgroupEntryEntity entry = new CdcOrdgroupEntryEntity();
                Long CdcOrdId = Long.parseLong(ordId);
                entry.setOrderGroupId(ordGroupId);
                entry.setOrderMastId(CdcOrdId);
                entry.setIsActive(1);
                add(entry);
            }
        }
        return R.ok();
    }

    /**
     * 删除医嘱套明细
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdcordgroupentry:delete")
    @BussinessLog(value = "删除医嘱套明细")
    public Object delete(@RequestParam Long cdcOrdgroupEntryId) {
	
        this.cdcOrdgroupEntryService.deleteById(cdcOrdgroupEntryId);

        return R.ok();
    }

    /**
     * 修改医嘱套明细
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdcordgroupentry:update")
    @BussinessLog(value = "修改医嘱套明细")
    public Object update(CdcOrdgroupEntryEntity cdcOrdgroupEntry) {
        if (CommonUtil.isOneEmpty(cdcOrdgroupEntry)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcOrdgroupEntryEntity old = this.cdcOrdgroupEntryService.selectById(cdcOrdgroupEntry.getId());

        old.setOrderMastId(cdcOrdgroupEntry.getOrderMastId());
        old.setOrderGroupId(cdcOrdgroupEntry.getOrderGroupId());
        old.setOrderRole(cdcOrdgroupEntry.getOrderRole());
        old.setIsActive(cdcOrdgroupEntry.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
